package com.ruoyi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州学院-计算机与信息工程学院
 * @ClassName ParseUtil
 * @Description 解析工具类
 * @Date 2022/07/13 17:01
 */
public class ParseUtil {

    /**
     * 将String[] -->  Long[]
     * @param str String[]
     * @return Long[]
     */
    public static long[] stringToLongArray(String[] str) {
        long[] arr = new long[str.length];
        for (int i = 0; i < str.length; i++) {
            arr[i] = Long.parseLong(str[i].trim());
        }
        return arr;
    }

    /**
     * 将String[] -->  List<Long>
     * @param str String[]
     * @return List<Long>
     */
    public static List<Long> stringToLongList(String[] str) {
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            list.add(Long.parseLong(str[i].trim()));
        }
        return list;
    }

    /**
     * 将List<Date> -->  List<String>
     * @param str List<String>
     * @return List<Date>
     */
    public static List<String> getDateNumberListToString(List<Date> str) {
        // 获得日期号
        SimpleDateFormat dateFm = new SimpleDateFormat("dd");
        List<String> list = new ArrayList<>();
        for (Date date : str) {
            list.add(dateFm.format(date));
        }
        return list;
    }


    /**
     * 获取改天是本年得第几周
     * @param date 日期
     * @return 周数
     */
    public static int getWeekDate(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int weekOfYear;
        try {
            Date date1 = format.parse(date);
            Calendar calendar = Calendar.getInstance();

            calendar.setFirstDayOfWeek(Calendar.MONDAY);

            calendar.setTime(date1);
            weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

        }catch(ParseException e)
        {
            e.printStackTrace();
            return  0;
        }
        return weekOfYear;
    }
}
